<?
@ini_set('session.use_trans_sid', 1);
session_start();


if (isset($_POST['language'])){
 if ($_POST['language']== "de")	$_SESSION['language']="de";
 else  				$_SESSION['language']="notde";   
}

if(isset($_POST['chattext'])) {
    if(isset($_SESSION['name'])){
	$text = $_POST['text'];
	$fp = fopen("log.html", 'a');
	fwrite($fp, "<div class='msgln'>(".date("G:i ").") <b>".$_SESSION['name']."</b>: ".stripslashes(htmlspecialchars($text))."<br></div>");
	fclose($fp);		}
}

if(isset($_POST['enter'])){
    if($_POST['name'] != "")	{
$_SESSION['name'] = stripslashes(htmlspecialchars($_POST['name']));
		
$empfaenger = "burkhart.holznagel@gmail.com";
$absendername = "in perchat";
$absendermail = "burkhart.holznagel@ba-berlin.com";
$timestamp = time();
$datum = date("d.m.Y",$timestamp);
$uhrzeit = date("H:i",$timestamp);
$betreff = "ui3";
$text = "Hallo ".$datum." ".$uhrzeit;
mail($empfaenger, $betreff, $text, "From: $absendername <$absendermail>");
				}
    else	
		echo '<spam class="error">Please type in a name</spam>';
}

if(isset($_GET['sound'])){
    if ($_SESSION['sound'] == "0") $_SESSION['sound'] = "1"; else $_SESSION['sound'] = "0";
}
if(isset($_GET['logout'])){
    if (isset( $_SESSION['name']))	{
	//Simple exit message
	$fp = fopen("log.html", 'a');
	fwrite($fp, "<div class='msgln'><i>User ". $_SESSION['name'] ." has left the chat session.</i><br></div>");
	fclose($fp);
    }
	unset($_SESSION['name']);
	unset($_SESSION['toggle']);
	session_destroy();
	header("Location: index.php"); //Redirect the user
	exit();
}

if(isset($_GET['clearchat'])){
    	$fp = fopen("log.html", 'w+');
    	fclose($fp);
}

if (isset($_POST['radio'])){
    if($_POST['radio'] == "0")	$_SESSION['textfile'] = "family.txt";
    else	{
	if($_POST['radio'] == "1")	$_SESSION['textfile'] = "eat.txt";
	else
	if($_POST['radio'] == "2")	$_SESSION['textfile'] = "bed.txt";
	    }
}

if(isset($_POST['myrow']))	{
    $file = fopen($_SESSION['textfile'], "r+") or exit("Unable to open file!".$_SESSION['textfile']);
    for ($i=0; $i <  $_SESSION['fileLines']; $i++)  	$_SESSION['text'][$i] = fgets($file,72);
    fclose($file);
    $file = fopen($_SESSION['textfile'], "w+") or exit("Unable to open file!".$_SESSION['textfile']);
    $i=$_POST['myrow'];
    $rev= strrev( $_SESSION['text'][$i]);
    if ( "$rev{0}" == "\n" )$_SESSION['text'][$i]=$_POST['mytext']; else $_SESSION['text'][$i]=$_POST['mytext']."\n";
    for ($i=0; $i < $_SESSION['fileLines']; $i++)  	fputs($file,$_SESSION['text'][$i],72);
    fclose($file); 
}


if (isset($_POST['alarmbutton']))	{
for ($i=0; $i <  $_SESSION['numMailAddresses']; $i++)  { 
    if (!preg_match('/^[^0-9][a-zA-Z0-9_]+([.][a-zA-Z0-9_-]+)*[@][a-zA-Z0-9_-]+([.][a-zA-Z0-9_-]+)*[.][a-zA-Z]{2,4}$/',
	$_SESSION['mailAddresses'][$i])) {
 echo "<spam class=\"error\">$i : invalid email address: ";echo $_SESSION["mailAddresses"][$i]; echo "<br></spam>";
        }
        else	{
    if (mail($_SESSION["mailAddresses"][$i], "Perchat",$_SESSION['mailText'], "From: "."perchat".$_SESSION["mailsender"])) 
    {echo "<spam class=\"error\">$i : mail to ";echo $_SESSION["mailAddresses"][$i]; echo "is accepted<br></spam>"; }
    else {
       echo "<spam class=\"error\">$i : mail to ";echo $_SESSION["mailAddresses"][$i]; echo "not accepted (perhaps server error?)<br></spam>";
    }
}

}
}
if(isset($_POST['mymail']))	{
    $file = fopen("mailaddress.txt", "r+") or exit("Unable to open file!"."mailaddress.txt");
    for ($i=0; $i <  $_SESSION['numMailAddresses']; $i++)  	$_SESSION['mailAddresses'][$i]= fgets($file,72);
    fclose($file);
    $file = fopen("mailaddress.txt", "w+") or exit("Unable to open file!"."mailaddress.txt");
    $i=$_POST['mymail'];
    $rev= strrev( $_SESSION['text'][$i]);
    if ( "$rev{0}" == "\n" )$_SESSION['mailAddresses'][$i]=$_POST['myaddress']; else $_SESSION['mailAddresses'][$i]=$_POST['myaddress']."\n";
    for ($i=0; $i <$_SESSION['numMailAddresses']; $i++)  fputs($file,$_SESSION['mailAddresses'][$i],72);
   fclose($file); 
}

if(isset($_POST['mymailtext']))	{
    $file = fopen("mailtext.txt", "w+") or exit("Unable to open file!"."mailtext.txt");
    $rev= strrev( $_SESSION['mailText']);
    if ( "$rev{0}" == "\n" )$_SESSION['mailText']=$_POST['mymailtext']; else $_SESSION['mailText']=$_POST['mymailtext']."\n";
    fputs($file,$_SESSION['mailText'],72);
   fclose($file); 
}

if(isset($_POST['mailsender']))	{
    $file = fopen("mailsender.txt", "w+") or exit("Unable to open file!"."mailsender.txt");
    $rev= strrev( $_SESSION['mailsender']);
    if ( "$rev{0}" == "\n" )$_SESSION['mailsender']=$_POST['mailsender']; else $_SESSION['mailsender']=$_POST['mailsender']."\n";
    fputs($file,$_SESSION['mailsender'],72);
   fclose($file); 
}


   if(isset($_POST['mode']))	{
    if($_POST['mode'] == "3letters")	{
	    $_SESSION['numLetters']=9;
	    $_SESSION['numButtons']=3;
    $_SESSION['charArray'][0]= array( "a","e","i","o","u","v","w","x","p");
    $_SESSION['charArray'][1]= array( "b","d","f","g","k","l","m","n","r");
    $_SESSION['charArray'][2]= array( "c","h","s","t","y","z","D","S","N");
	
    }
    
    if($_POST['mode'] == "moreletters")	{
	       $_SESSION['numLetters']=5;
	       $_SESSION['numButtons']=5;
    if ($_SESSION['actualLetterNumber']>= $_SESSION['numLetters']) $_SESSION['actualLetterNumber']=0;
    $_SESSION['charArray'][0]= array( "a","e","i","o","u");
    $_SESSION['charArray'][1]= array( "b","d","f","g","j");
    $_SESSION['charArray'][2]= array( "k","l","m","n","p");
    $_SESSION['charArray'][3]= array( "r","s","t","c","h");
    $_SESSION['charArray'][4]= array( "v","w","x","y","z");
     }
    }
?>